package com.yuven.appframework.base

import android.content.Intent
import android.view.View
import androidx.annotation.CallSuper
import androidx.annotation.ColorRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.yuven.appframework.R
import com.yuven.appframework.databinding.CommonCoreViewRootBinding
import com.yuven.appframework.widget.titlebar.OnTileClickListener
import com.yuven.appframework.widget.titlebar.StandardTitleBar
import com.yuven.baselib.component.BaseMVVMActivity
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.ViewUtils
import com.yuven.appframework.manager.NetWorkManager
import com.yuven.appframework.manager.listener.INetWorkConnectListener
import kotlinx.android.synthetic.main.common_toolbar.*
import kotlinx.android.synthetic.main.common_core_view_root.*

/**
 *
 * 提供标准title能力的AC 基于MVVM编码规范，//TODO 能力下沉到base-core
 * @author :        Yuven
 * @date :          2021/7/29 9:19 上午
 * @desc :
 */
abstract class BaseStdTitleActivity<VM : BaseViewModel?, DB : ViewDataBinding?> :
    BaseMVVMActivity<VM, DB>(), INetWorkConnectListener {

    final override fun layoutId(): Int {
        return R.layout.common_core_view_root;
    }

    @CallSuper
    override fun initView() {
        if (enableToolbar()) {
            initToolbar()
        }
        initRealContentView()
        if (enableLightStatusBar()) {
            ViewUtils.setAndroidNativeLightStatusBar(this, true)
        }
        ViewUtils.setStatusBarColor(this, statusBarColor())
    }


    private fun initRealContentView() {
        view_stub_content?.layoutResource = contentLayoutId()
        var contentView =
            (dataBinding as CommonCoreViewRootBinding).viewStubContent.viewStub?.inflate()
        dataBinding = contentView?.let { DataBindingUtil.bind<DB>(it) }
    }

    private fun initToolbar() {
        view_stub_toolbar.layoutResource = toolbarLayoutId()
        view_stub_toolbar.visibility = View.VISIBLE
        getToolBarTitle()?.let { common_titleBar.setTitle(it) }
        common_titleBar.setOnTitleBarListener(object : OnTileClickListener {
            override fun onTitleClick(view: View?) {
                onToolbarTitleTvClick()
            }

            override fun onTitleIconClick(view: View?) {
            }

            override fun onLeftClick(view: View?) {
            }

            override fun onLeftIconClick(view: View?) {
                onToolbarLeftIconClick()
            }

            override fun onRightClick(view: View?) {
                onToolbarRightTvClick()
            }

            override fun onRightIconClick(view: View?) {
                onToolbarRightIconClick()
            }

        })

        common_titleBar.setTitleBarColor(titleBarBackGroundColor());
    }

    protected open fun toolbarLayoutId(): Int {
        return R.layout.common_toolbar
    }

    @CallSuper
    override fun onResume() {
        super.onResume()
        if (registerNetworkMonitor()) {
            NetWorkManager.getInstance().registerListener(this)
        }
    }

    @CallSuper
    override fun onPause() {
        super.onPause()
        if (registerNetworkMonitor()) {
            NetWorkManager.getInstance().unRegisterListener(this);
        }
    }

    /**
     * 需要监听网络 重写该方法 并返回true
     *
     * @return
     */
    protected open fun registerNetworkMonitor(): Boolean {
        return false
    }

    override fun onDisconnect() {
    }

    final override fun onConMobileNet() {
        onNetConnectRecovery()
    }

    final override fun onConWifiNet() {
        onNetConnectRecovery()
    }

    /**
     * 网络恢复
     */
    open fun onNetConnectRecovery() {
    }

    /**
     * 内容布局实现
     */
    abstract fun contentLayoutId(): Int

    @ColorRes
    protected open fun titleBarBackGroundColor(): Int {
        return android.R.color.transparent
    }

    @ColorRes
    protected open fun statusBarColor(): Int {
        return android.R.color.transparent
    }

    open fun enableToolbar(): Boolean {
        return true
    }

    protected fun enableLightStatusBar(): Boolean {
        return true
    }

    //toolbar返回icon点击
    protected open fun onToolbarLeftIconClick() {
        finish()
    }

    //toolbar右边文字点击
    protected open fun onToolbarRightTvClick() {

    }

    //toolbar标题点击
    protected open fun onToolbarTitleTvClick() {

    }

    //toolbar右边图标点击
    protected open fun onToolbarRightIconClick() {

    }

    protected fun getTitleBarLayout(): StandardTitleBar {
        return common_titleBar
    }

    override fun initIntent(intent: Intent?) {

    }

    abstract fun getToolBarTitle(): String?

}